﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Collections;
using System.Data;

namespace PB.FBMembership.DAL
{
    /// <summary>
    /// Provides database functionalities
    /// </summary>
    public static class DBFacade
    {


        #region Public Methods

        /// <summary>
        /// Executes specified non-query stored proc with given parameters.
        /// </summary>
        /// <param name="spname">Name of the Stored Procedure to be executed.</param>
        /// <param name="parameters">name-value pairs for parameters to specified stored proc.</param>
        /// <returns>number of rows affected</returns>
        public static int ExecuteNonQuery(string spname, Hashtable parameters)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase();

                DbCommand dbCmd = db.GetStoredProcCommand(spname);

                ParameterFactory.CreateParameters(db, dbCmd, parameters);

                return db.ExecuteNonQuery(dbCmd);
            }
            catch (Exception ex)
            {
                throw new DataException("Error while ExecutingNonQuery in DBFacade for " + spname, ex);
            }
        }

        /// <summary>
        /// Executes specified query stored proc with given parameters and returns IDataReader.
        /// </summary>
        /// <param name="spname">Name of the Stored Procedure to be executed.</param>
        /// <param name="parameters">name-value pairs for parameters to specified stored proc.</param>
        /// <returns></returns>
        public static IDataReader ExecuteDataReader(string spname, Hashtable parameters)
        {
            try
            {
                Database db = DatabaseFactory.CreateDatabase();

                DbCommand dbCmd = db.GetStoredProcCommand(spname);

                ParameterFactory.CreateParameters(db, dbCmd, parameters);

                return db.ExecuteReader(dbCmd);
            }
            catch (Exception ex)
            {
                throw new DataException("Error while ExecuteDataReader in DBFacade for " + spname, ex);
            }

        }
        #endregion






    }
}
